clear matrix
clear mata
set maxvar 10000 
set matsize 11000
capture log close

capture program drop rinf
program define rinf, rclass
	syntax [anything], esttype(string) dv(string) iter(numlist>0) treats(string) numtreat(numlist>0) absorb(string)  cluster(string) lr(numlist>0) lr1(string)

cd "~/Dropbox/nepal_localgovt/analysis/"
global repl_dir "jop/replication archive"
global rinf_dir "${repl_dir}"

qui{ 
	if "`absorb'" == " " {
		local abs 
	}
	else {
		local abs absorb(`absorb')
	}
	if "`cluster'" == " " {
		local clust 
	}
	else {
		local clust cluster(`cluster')
	}
	
preserve
use "${repl_dir}/nepal_final_cleaned.dta", clear
 
*************************

merge m:1 seatid using "${rinf_dir}/rinf_vector.dta", keepusing(treat*)
	drop _merge

*drop if missing(vshare)

rename treat treattrue

forvalues i= 1/`iter' {
noi di in red "Currently On Iteration `i'"
di in yellow `i'

capture drop treat
drop treat_* 
rename treat`i' treat

gen treat_elect = (inlist(treat, 3, 4, 7, 8))
gen treat_loyal = (inlist(treat, 5, 6, 7, 8))
gen treat_comp = (inlist(treat, 2, 4, 6, 8))

gen treat_elect_change = (treat_elect==1 & signal_change==1)
gen treat_elect_nochange = (treat_elect==1 & signal_none==1)
gen treat_elect_pos = (treat_elect==1 & signal_pos==1)
gen treat_elect_neg = (treat_elect==1 & signal_neg==1)
gen treat_elect_magn = treat_elect*signal_magn
gen treat_electXpos_mag = treat_elect*pos_mag
gen treat_electXneg_mag = treat_elect*neg_mag
gen treat_electXblock = treat_elect*block
gen treat_elect_climb_abs = climb_abs* treat_elect
gen treat_elect_climb_neg = climb_sign_id1* treat_elect
gen treat_elect_climb_noch = climb_sign_id2* treat_elect
gen treat_elect_climb_pos = climb_sign_id3* treat_elect
		
		if `esttype' == 0{
		reg `dv' `treats', `abs' `clust' r
		mat tab = r(table)
		cap lincom `lr1'
		cap local estl = `r(estimate)'
			}	
		if `esttype' == 3{
		reg `dv' `treats', nocons  `clust' r
		mat tab = r(table)
		cap lincom `lr1'
		cap local estl = `r(estimate)'
			}
		if `esttype' == 1 {
		reghdfe `dv' `treats', `abs' `clust'
		mat tab = r(table)
		cap lincom `lr1'
		cap local estl = `r(estimate)'

		}
		if `lr' ==2 {
			
			if `i' == 1 {
			mat b = tab[1,1..`numtreat']
				}
			else {
				mat b = b \ tab[1,1..`numtreat']
				}
		}
		else {
			if `i' == 1 mat b = [`estl']
			else mat b = b \ [`estl']

		}
	
	}

	
drop treat_*
drop treat
rename treattrue treat

gen treat_elect = (inlist(treat, 3, 4, 7, 8))
gen treat_loyal = (inlist(treat, 5, 6, 7, 8))
gen treat_comp = (inlist(treat, 2, 4, 6, 8))

gen treat_elect_change = (treat_elect==1 & signal_change==1)
gen treat_elect_nochange = (treat_elect==1 & signal_none==1)
gen treat_elect_pos = (treat_elect==1 & signal_pos==1)
gen treat_elect_neg = (treat_elect==1 & signal_neg==1)
gen treat_elect_magn = treat_elect*signal_magn
gen treat_electXpos_mag = treat_elect*pos_mag
gen treat_electXneg_mag = treat_elect*neg_mag
gen treat_electXblock = treat_elect*block
gen treat_elect_climb_abs = climb_abs* treat_elect
gen treat_elect_climb_neg = climb_sign_id1* treat_elect
gen treat_elect_climb_noch = climb_sign_id2* treat_elect
gen treat_elect_climb_pos = climb_sign_id3* treat_elect


		if `esttype' == 0{
		reg `dv' `treats', `abs' `clust' r
		mat tab = r(table)
		cap lincom `lr1'
		cap local estl = `r(estimate)'
			}	
		if `esttype' == 3{
		reg `dv' `treats', nocons  `clust' r
		mat tab = r(table)
		cap lincom `lr1'
		cap local estl = `r(estimate)'
			}
		if `esttype' == 1  {
		reghdfe `dv' `treats', `abs' `clust'
		mat tab = r(table)
		cap lincom `lr1'
		cap local estl = `r(estimate)'

		}
	if `lr' ==2 {

		forvalues j = 1/`numtreat'{
		local b_true`j' = tab[1,`j']
		}
		svmat b, names(bval)
		keep bval*
		drop if missing(bval1)
	forvalues j = 1/`numtreat'{
			gen b_true`j' = `b_true`j''

		}
		}
		else {

		local b_true1 = `estl'

		svmat b, names(bval)
		keep bval*
		drop if missing(bval1)
			
		gen b_true1 = `b_true1'

		}

* Calculating pvalue

forvalues i = 1/`numtreat' {
	gen count`i' = 0
	sum bval`i'
	if b_true`i' <`r(mean)' {
		replace count`i'=1 if b_true`i'>bval`i'
		}
	else {
		replace count`i'=1 if b_true`i'<bval`i'
		}
	sum count`i'
	local pval`i' = `r(mean)'

	
	if `pval`i'' <= 1 local pexact`i' = `pval`i''
	else local pexact`i' == 1
		
			global p`i' = `pexact`i''
			noi di in red "`pexact`i''"
	}
restore
	}
end
